package com.speedy.course.ext

import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import android.widget.Toast
import androidx.annotation.ArrayRes
import androidx.annotation.ColorRes
import androidx.annotation.DimenRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import java.io.BufferedReader
import java.io.InputStreamReader


/**
 * @desc context扩展实现 方便调用
 */
fun Context.resString(@StringRes id: Int, vararg formatArgs: String?): String {
    return resources.getString(id, *formatArgs)
}

fun Context.resString(@StringRes id: Int): String {
    return resources.getString(id)
}

fun Context.resDimen(@DimenRes id: Int): Int {
    return resources.getDimensionPixelOffset(id)
}

fun Context.resDrawable(@DrawableRes id: Int): Drawable? {
    return ContextCompat.getDrawable(this, id)
}

fun Context.resStringArray(@ArrayRes id: Int): Array<String> {
    return resources.getStringArray(id)
}

fun Context.resColor(@ColorRes id: Int): Int {
    return ContextCompat.getColor(this, id)
}

fun Context.toast(msg: String) {
    Toast.makeText(this.applicationContext, msg, Toast.LENGTH_SHORT).show()
}

fun Fragment.resString(@StringRes id: Int): String {
    return resources.getString(id)
}

fun Fragment.resString(@StringRes id: Int, vararg formatArgs: String?): String {
    return resources.getString(id, *formatArgs)
}

fun Fragment.resDimen(@DimenRes id: Int): Int {
    return resources.getDimensionPixelOffset(id)
}

fun Fragment.resColor(@ColorRes id: Int): Int {
    return ContextCompat.getColor(requireContext(), id)
}

fun Fragment.resStringArray(@ArrayRes id: Int): Array<String> {
    return resources.getStringArray(id)
}

fun Fragment.toast(msg: String): Unit {
    Toast.makeText(this.context, msg, Toast.LENGTH_SHORT).show()
}

fun Activity.toast(msg: String): Unit {
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
}


/**
 * 根据手机的分辨率将dp转成为px。
 */
fun Context.dp2px(dp: Float): Int {
    val scale = this.resources.displayMetrics.density
    return (dp * scale + 0.5f).toInt()
}

/**
 * 根据手机的分辨率将px转成dp。
 */
fun Context.px2dp(px: Float): Int {
    val scale = this.resources.displayMetrics.density
    return (px / scale + 0.5f).toInt()
}

/**
 * 获取屏幕宽值。
 */
fun Context.screenWidth(): Int {
    return resources.displayMetrics.widthPixels
}

/**
 * 获取屏幕高值。
 */
fun Context.screenHeight(): Int {
    return resources.displayMetrics.heightPixels
}


/**
 * 获取屏幕像素：对获取的宽高进行拼接。例：1080X2340。
 */
fun Context.screenPixel(): String {
    this.resources.displayMetrics.run {
        return "${widthPixels}X${heightPixels}"
    }
}

fun Fragment.detachSelf() {
    try {
        parentFragmentManager.beginTransaction()
            .remove(this)
            .commitAllowingStateLoss()
    } catch (e:Exception) {
        //ignore
    }
}

inline fun <reified T> Fragment.findInstanceParent() : T? {
    var parent = parentFragment
    while (parent != null) {
        if (parent is T) {
            return parent
        }
        parent = parent.parentFragment
    }
    val activity = activity
    if (activity is T) {
        return activity
    }
    return null
}

/**
 * 读取 Asset 文件中的内容
 */
fun Context.readAsset(assetsFileName: String): String {
    return BufferedReader(InputStreamReader(resources.assets.open(assetsFileName))).use {
        val sb = StringBuilder()
        it.forEachLine { s ->
            sb.append(s)
        }
        sb.toString()
    }
}

